/**
 * 
 */
package de.cbf.cam.dsa.exchange.ownxml;

import java.beans.XMLDecoder;
import java.beans.XMLEncoder;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.OutputStream;

import de.cbf.cam.dsa.exchange.interfaces.IDSAImporter;
import de.cbf.cam.dsa.exchange.ownxml.descriptions.CharacterDescription;
import de.cbf.cam.dsa.model.entities.OneNPC;

/**
 * @author Stefan Alter
 *
 */
public class XMLImporter implements IDSAImporter {

	private static final String name = "CAM XML-Importer";
	/**
	 * 
	 */
	public XMLImporter() {		
	}

	/* (non-Javadoc)
	 * @see de.cbf.cam.dsa.exchange.interfaces.IDSAImporter#getName()
	 */
	@Override
	public String getName() {
		return name;
	}

	/* (non-Javadoc)
	 * @see de.cbf.cam.dsa.exchange.interfaces.IDSAImporter#openView()
	 */
	@Override
	public void openView() {
		// TODO Auto-generated method stub

	}

	public static OneNPC importCharacter(String fileName) throws FileNotFoundException{
		FileInputStream inputStream;		
		inputStream = new FileInputStream(fileName);
	
		XMLDecoder decoder = new XMLDecoder(inputStream);
		CharacterDescription description = (CharacterDescription)decoder.readObject();
		decoder.close();
		
		return description.getCharacter();
	}
	public static void export(Object object,String fileName) throws FileNotFoundException{
		FileOutputStream os = new FileOutputStream(fileName);
		XMLEncoder encoder = new XMLEncoder(os);
		
		encoder.writeObject(object);
		encoder.close(); 
	}
	
	public static void export(Object object,OutputStream os ) {
		XMLEncoder encoder = new XMLEncoder(os);
		
		encoder.writeObject(object);
		encoder.close(); 
	}
	
	public static void exportCharacter(OneNPC character,String fileName) throws FileNotFoundException{
		CharacterDescription description = new CharacterDescription(character);
		export(description,fileName);
	}
}
